import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemMenuApi } from '#/api';

import { z } from '@vben/common-ui';

import { getMenuList } from '#/api/system';
import { $t } from '#/locales';

export function getMenuTypeOptions() {
  return [
    {
      color: 'processing',
      label: $t('system.menu.typeCatalog'),
      value: 'catalog',
    },
    { color: 'default', label: $t('system.menu.typeMenu'), value: 'menu' },
    { color: 'error', label: $t('system.menu.typeButton'), value: 'button' },
  ];
}

function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: getMenuTypeOptions(),
        optionType: 'button',
      },
      defaultValue: 'catalog',
      fieldName: 'menu_type',
      label: $t('system.menu.type'),
      rules: 'required',
      formItemClass: 'col-span-2', // 跨两列显示
    },
    {
      component: 'Input',
      fieldName: 'code',
      label: $t('system.menu.code'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.menu.code'), 2]))
        .max(50, $t('ui.formRules.maxLength', [$t('system.menu.code'), 20])),
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.menu.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.menu.name'), 2]))
        .max(20, $t('ui.formRules.maxLength', [$t('system.menu.name'), 20])),
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: getMenuList,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        style: { width: '100%' },
        dropdownStyle: { maxHeight: '400px', overflow: 'auto' },
      },
      fieldName: 'parent_id',
      label: $t('system.menu.parentMenu'),
      dependencies: {
        rules: (values) => {
          // 按钮类型时组件项必填
          return values.menu_type === 'button' ? 'required' : null;
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'Input',
      fieldName: 'path',
      label: $t('system.menu.path'),
      rules: z
        .string()
        .min(1, $t('ui.formRules.minLength', [$t('system.menu.path'), 1]))
        .max(100, $t('ui.formRules.maxLength', [$t('system.menu.path'), 100])),
      dependencies: {
        show: (values) => {
          // 目录和按钮时隐藏组件项
          return values.menu_type !== 'button';
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'Input',
      fieldName: 'component',
      label: $t('system.menu.component'),
      dependencies: {
        show: (values) => {
          // 目录和按钮时隐藏组件项
          return values.menu_type === 'menu';
        },
        rules: (values) => {
          // 菜单类型时组件项必填
          return values.menu_type === 'menu' ? 'required' : null;
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'IconPicker',
      componentProps: {
        prefix: 'carbon',
      },
      fieldName: 'icon',
      label: $t('system.menu.icon'),
      dependencies: {
        show: (values) => {
          // 按钮类型时隐藏图标项
          return values.menu_type !== 'button';
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'Input',
      fieldName: 'title',
      label: $t('system.menu.titleLabel'),
      dependencies: {
        show: (values) => {
          // 按钮类型时隐藏标题项
          return values.menu_type !== 'button';
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'Input',
      fieldName: 'redirect',
      label: $t('system.menu.redirect'),
      dependencies: {
        show: (values) => {
          // 按钮类型时隐藏重定向项
          return values.menu_type !== 'button';
        },
        triggerFields: ['menu_type'],
      },
    },
    {
      component: 'InputNumber',
      componentProps: {
        min: 0,
        max: 999,
      },
      defaultValue: 0,
      fieldName: 'order',
      label: $t('system.menu.order'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.menu.status'),
    },
    {
      component: 'Switch',
      fieldName: 'is_hidden',
      label: $t('system.menu.isHidden'),
    },
    {
      component: 'Switch',
      fieldName: 'keepalive',
      label: $t('system.menu.keepalive'),
      defaultValue: true,
      dependencies: {
        show: (values) => {
          // 目录和按钮类型时隐藏缓存项
          return values.menu_type === 'menu';
        },
        triggerFields: ['menu_type'],
      },
    },
  ];
}

function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'code',
      label: $t('system.menu.code'),
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.menu.name'),
    },
  ];
}

function useColumns<T = SystemMenuApi.Menu>(
  onActionClick?: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('system.menu.name'),
      treeNode: true,
      minWidth: 180,
      resizable: true,
    },
    {
      field: 'code',
      title: $t('system.menu.code'),
      minWidth: 120,
      resizable: true,
    },
    {
      cellRender: { name: 'CellTag', options: getMenuTypeOptions() },
      field: 'menu_type',
      title: $t('system.menu.type'),
      width: 100,
    },
    {
      field: 'path',
      title: $t('system.menu.path'),
      minWidth: 150,
      showOverflow: 'tooltip',
      resizable: true,
    },
    {
      field: 'icon',
      title: $t('system.menu.icon'),
      width: 100,
      showOverflow: 'tooltip',
      visible: false, // 默认隐藏，可通过列设置显示
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'success', label: $t('common.enabled'), value: 1 },
          { color: 'error', label: $t('common.disabled'), value: 0 },
        ],
      },
      field: 'status',
      title: $t('system.menu.status'),
      width: 80,
    },
    {
      field: 'order',
      title: $t('system.menu.order'),
      width: 70,
    },
    {
      field: 'created_at',
      title: $t('system.menu.createTime'),
      width: 160,
      formatter: 'formatDateTime',
      visible: false, // 默认隐藏，可通过列设置显示
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.menu.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'append',
            text: $t('system.menu.addChild'),
          },
          'edit',
          'delete',
        ],
      },
      field: 'operation',
      fixed: 'right',
      title: $t('common.operation'),
      width: 160,
    },
  ];
}

export { useColumns, useFormSchema, useGridFormSchema };
